/**
 * <p>gridpanel2的控制类</p>
 * 继承自Ext.app.Controller

 * @module Ext-ux-cceaf-controller
 * @class cceaf.controller.GridPanelCtrl2
 * @extends Ext.app.Controller
 * @since 1.0
 */
Ext.define('cceaf.controller.GridPanelCtrl2',{
	extend: 'Ext.app.Controller', 
	requires: [
	    'cceaf.control.StatGridPanel'
    ],
	
	
    constructor: function(config){
		config = config || {};
		
		config = Ext.applyIf(config || {}, {

		});

		cceaf.controller.GridPanelCtrl2.superclass.constructor.apply(this, arguments);
	},

	/**
     * 监听事件
     */
    init: function() {
    	//控制层需控制视图的xtype
    	var controlledViewXType = this.controlledViewXType;
    	if (!controlledViewXType) {
    		//控制名称
    		var className = this.self.getName();
    		alert('必须设置控制层' + className + '需控制视图的xtype，即controlledViewXType属性！');
    		return;
    	}
    	
    	//构建初始化的事件监听
    	var eventTemplate = "this.control({" + 
	    	"'aaaaa': {" +
	    	"	afterrender: this.initPage" +
	        "}," +
	        "'aaaaa >pagingtoolbar #refresh': {" +
            "    click: this.page_refresh" +
            "}," + 
    		"'aaaaa #btnQuery': {" +
            "    click: this.page_query" +
            "}," + 
            "'aaaaa #btnReset': {" +
            "    click: this.page_reset" +
            "}," + 
        	"'aaaaa #btnAdd': {" + 
            "    click: this.page_add" + 
            "}," + 
            "'aaaaa #btnEdit': {" + 
            "    click: this.page_edit" + 
            "}," + 
            "'aaaaa #btnView': {" + 
            "    click: this.page_view" + 
            "}," + 
            "'aaaaa #btnDelete': {" + 
            "    click: this.page_delete" + 
            "}," + 
            "'aaaaa #btnWriteOff': {" + 
            "    click: this.page_writeOff" + 
            "}," + 
            "'aaaaa #btnStat': {" + 
            "    click: this.page_stat" + 
            "}," + 
            "'aaaaa #btnPrintBill': {" + 
            "    click: this.printBill" + 
            "}," + 
            "'aaaaa #btnPrintList': {" + 
            "    click: this.printList" + 
            "}," + 
            "'aaaaa #btnExportExcel': {" + 
            "    click: this.exportExcel" + 
            "}" + 
    	"});";
	    
	    var eventString = eventTemplate.replace(/aaaaa/gi, controlledViewXType);
	    eval(eventString);
    	
        this.registerEvent();
        
      //标记已经初始化完成
        this.isInit = true;
    },
    
    /**
     * 对于动态加载controller模式
     * 第一次加载后设置为true，再次加载后不在执行init方法 
     * 
     * @property isInit 
     * @type Boolean
     * @default false
     */
    isInit: false,
    
    /**
     * 监听其他事件
     */
    registerEvent: function(){
    	
    },
	
    /**
	 * 获取视图
	 *
     * @return {Ext.container.Container} 视图对象
     */
    getControlledView: function(){
    	var controlledViewXType = this.controlledViewXType;
    	var methodName = 'this.get' + controlledViewXType.substr(0,1).toUpperCase() + controlledViewXType.substring(1) + '()';
    	//alert(methodName);
    	
    	var view = eval(methodName);
    	return view;
    },
    
    /**
     * 查询前检查
     * 
     * @return {boolean}
     */
    preQuery: function(){
    	var queryGrid = this.getControlledView();

    	var model = queryGrid.model;
    	if (!model) {
			alert('请设置gridpanel的model！');
			return false;
		}
    	
    	if(!Ext.ModelManager.getModel(model)) {
    		alert(queryGrid.getXType() + "的" + model + '不存在！');
    		return false;
    	}
    	
    	//检查查询条件,调用页面需重载
    	if(!this.checkQueryCondition()){
    		return false;
    	}

    	var queryParams = {};
    	var queryAreaItems = queryGrid.query('textfield');
    	for(var i=0;queryAreaItems != null && i<queryAreaItems.length;i++){
    		var item = queryAreaItems[i];
    		var itemValue = item.getValue();
    		
    		var spiltIndex = item.getName().lastIndexOf('-');
    		if (spiltIndex != -1) {
    			cceaf.util.ExtHelper.showInfoDialog('查询条件必须设置name属性！');
    			return false;
    		}
    		
    		var itemPrefixIndex = item.getName().lastIndexOf('q_');
			if(itemPrefixIndex == 0){
				if(item.getValue() == null){
					eval("queryParams." + item.getName() + " = ''");
				}
				else{
					if(Ext.isDate(itemValue)){
						itemValue = Ext.Date.format(itemValue,'Y-m-d');
					}
					eval("queryParams." + item.getName() + " = '" + itemValue + "'");
				}
			}
    	}
    	
    	/*
    	queryAreaItems.each(function(item){
    		itemPrefixIndex = item.getId().lastIndexOf('q_');
			if(itemPrefixIndex == 0){
				eval("queryParams." + item.getId() + " = '" + item.getValue() + "'");
			}
		});*/
    	queryGrid.getStore().proxy.extraParams = queryParams;
    	
    	return true;
    },
    
    /**
     * 检查查询条件，调用页面如有检查如重载此方法
     */
    page_refresh: function(){
    	//检查查询条件,调用页面需重载
    	if(!this.checkQueryCondition()){
    		return false;
    	}
    	
    	var queryGrid = this.getControlledView();
    	var toolbarpaging = queryGrid.down('cceaftoolbarpaging');
    	var me = toolbarpaging,
    				current = me.store.currentPage;
		
        if (me.fireEvent('beforechange', me, current) !== false) {
            me.store.loadPage(current);
        }
    },
    
	/**
     * 查询对象
     */
    page_query: function(){
    	if (!this.preQuery()) {
    		return;
    	}
    	
    	var queryGrid = this.getControlledView();

    	//重新定位到第一页
    	queryGrid.getStore().currentPage = 1;
    	queryGrid.getStore().load({params:{isquery:true}});
    },
	
    /**
     * 检查查询条件，调用页面如有检查如重载此方法
     */
    checkQueryCondition: function(){
    	return true;
    },
    
    
    /**
     * 重置查询条件
     */
    page_reset: function(){
    	var queryGrid = this.getControlledView();
    	
    	var queryAreaItems = queryGrid.query('textfield');
    	/*
    	queryAreaItems.forEach(function(item){
    		itemPrefixIndex = item.getId().lastIndexOf('q_');
			if(itemPrefixIndex == 0){
				item.setValue("");
			}
		});*/
    	
    	for(var i=0;queryAreaItems != null && i<queryAreaItems.length;i++){
    		var item = queryAreaItems[i];
    		itemPrefixIndex = item.getName().lastIndexOf('q_');
			if(itemPrefixIndex == 0){
				item.setValue("");
			}
    	}
    },
    
    /**
     * 新增对象
     */
    page_add: function(){
    	var controller = this;
    	var queryGrid = controller.getControlledView();
    	
    	var popWindow = queryGrid.popWindow;
		var popWindowWidth = popWindow.width;
		var popWindowHeight = popWindow.height;
		if (!popWindowWidth) {
			popWindowWidth = popWindowHeight/0.618;//黄金分割
		}
		if (!popWindowHeight) {
			popWindowHeight = popWindowWidth*0.618;//黄金分割
		}
		
		if (controller.checkAddPage() == false) {
			return;
		}
		
    	//修正IE上的BUG
//    	if (Ext.isIE) {
//    		popWindowWidth += 35;
//    	}
    	
    	//设置页面状态为：新增页面
    	queryGrid.pageStatus = 'add';
    	var title = '新增' + queryGrid.moduleName;
    	
    	//var billForm = queryGrid.billForm;
    	if(queryGrid.newFormCreate == true || queryGrid.billForm == null){
    		queryGrid.createForm();
    	}
    	
    	
    	queryGrid.billForm.form.reset();
    	queryGrid.billForm.setReadOnly(false);
    	queryGrid.billForm.storeLoad = true;
    	
    	var billGrid = queryGrid.billForm.down('gridpanel');
		billGrid.getStore().removeAll();
		var btnDeleteRow = billGrid.down('#btnDeleteRow');
		if (btnDeleteRow) {
			btnDeleteRow.setVisible(true);
		}
		var btnAddRow = billGrid.down('#btnAddRow');
		if (btnAddRow) {
			btnAddRow.setVisible(true);
		}
    	
		var btnSave = queryGrid.billForm.down('[itemId=btnSave]');
		if (btnSave) {
			btnSave.setVisible(true);
		}

		if (queryGrid.cardLayout) {
			var cardPanel = queryGrid.up('panel');
			//alert(cardPanel.items.length);
			cardPanel.add(queryGrid.billForm);
			cardPanel.getLayout().setActiveItem(1);
		} else {
			new Ext.Window({
	        	title: title,
	        	modal: true,
	        	width: popWindowWidth,
	        	height: popWindowHeight,
	        	autoScroll: true,
	        	layout: 'fit',
	        	plain: true,
	        	closeAction: 'hide', //可能会造成内存泄漏，但如果不用hide会出错，估计是extjs的bug
	        	items: [queryGrid.billForm]
	    	}).show();
		}
    	
    	//设置初始值
    	controller.initAddPage();
    },
    
    /**
     * 编辑对象
     */
    page_edit: function(){
    	var controller = this;
    	var queryGrid = controller.getControlledView();
    	
    	var popWindow = queryGrid.popWindow;
		var popWindowWidth = popWindow.width;
		var popWindowHeight = popWindow.height;
		if (!popWindowWidth) {
			popWindowWidth = popWindowHeight/0.618;//黄金分割
		}
		if (!popWindowHeight) {
			popWindowHeight = popWindowWidth*0.618;//黄金分割
		}
		
		if (controller.checkEditPage() == false) {
			return;
		}
		
    	//修正IE上的BUG
//    	if (Ext.isIE) {
//    		popWindowWidth += 35;
//    	}
    	
    	var selModel = queryGrid.getSelectionModel();
    	
    	//设置页面状态为：编辑页面
    	queryGrid.pageStatus = 'edit';
    	var title = '编辑' + queryGrid.moduleName;
    	
   	 	var selectedCount = selModel.getCount();
    	if(selectedCount == 0){
    		cceaf.util.ExtHelper.showInfoDialog('请选择编辑的行');
    	}
    	else if(selectedCount > 1){
    		cceaf.util.ExtHelper.showInfoDialog('只能选择一行编辑');
    	}
    	else{
    		//得到编辑对象的主键
    		var masterkeys = queryGrid.masterKeyIds.split(',');
    		var url = queryGrid.findByKeyAction + '?';
    		for(var i=0;i<masterkeys.length;i++){
    			if(masterkeys[i] == null || masterkeys[i] == ''){
    				continue;
    			}
    			var temp = selModel.getLastSelected().get(masterkeys[i]);
    			url += masterkeys[i] + '=' + temp + '&';
    		}
    		
			//var masterkey = selModel.getLastSelected().get(queryGrid.masterKeyId);
			//var billForm = queryGrid.billForm;
    		if(queryGrid.newFormCreate == true || queryGrid.billForm == null){
	    		queryGrid.createForm();
	    	}
	    	
			queryGrid.billForm.form.reset();
			queryGrid.billForm.setReadOnly(false);
			queryGrid.billForm.storeLoad = true;
			
			var billGrid = queryGrid.billForm.down('gridpanel');
			billGrid.getStore().removeAll();
			var btnDeleteRow = billGrid.down('#btnDeleteRow');
			if (btnDeleteRow) {
				btnDeleteRow.setVisible(true);
			}
			var btnAddRow = billGrid.down('#btnAddRow');
			if (btnAddRow) {
				btnAddRow.setVisible(true);
			}
			
			var btnSave = queryGrid.billForm.down('[itemId=btnSave]');
			if (btnSave) {
				btnSave.setVisible(true);
			}
			
			if (queryGrid.cardLayout) {
				var cardPanel = queryGrid.up('panel');
				//alert(cardPanel.items.length);
				cardPanel.add(queryGrid.billForm);
				cardPanel.getLayout().setActiveItem(1);
			} else {
	    		new Ext.Window({
		        	title: title,
		        	modal: true,
		        	width: popWindowWidth,
		        	height: popWindowHeight,
		        	autoScroll:true,
		        	closeAction: 'hide',
		        	layout: 'fit',
		        	items: [queryGrid.billForm]
			    }).show();
			}
        	
        	//装载对象到表单
        	queryGrid.billForm.form.load({
        		url:  url,
		        waitMsg : '正在载入数据...',
		        success: function(form,action) { 
		        	//表单载入数据时,初始化其他操作	  
		        	controller.loadDetailsData(action);
		        	controller.initEditPage(action);
		        	
		        	//显示正确提示
		        	var jsonObject =eval("(" + action.response.responseText + ")");
		        	cceaf.util.ExtHelper.showInfoDialog(jsonObject.message);
		        },
		        failure: function(form,action) {
		        	//显示错误提示
		        	var jsonObject =eval("(" + action.response.responseText + ")");
		        	cceaf.util.ExtHelper.showErrorDialog(jsonObject.message);
		        }
		   	});
    	}
    },
    
    /**
     * 查看对象
     */
    page_view: function(){
    	var controller = this;
    	var queryGrid = controller.getControlledView();
    	
    	var popWindow = queryGrid.popWindow;
		var popWindowWidth = popWindow.width;
		var popWindowHeight = popWindow.height;
		if (!popWindowWidth) {
			popWindowWidth = popWindowHeight/0.618;//黄金分割
		}
		if (!popWindowHeight) {
			popWindowHeight = popWindowWidth*0.618;//黄金分割
		}
		
		if (controller.checkViewPage() == false) {
			return;
		}
		
    	//修正IE上的BUG
//    	if (Ext.isIE) {
//    		popWindowWidth += 35;
//    	}
    	
    	var selModel = queryGrid.getSelectionModel();
    	
    	//设置页面状态为：编辑页面
    	queryGrid.pageStatus = 'view';
    	var title = '查看' + queryGrid.moduleName;
    	
   	 	var selectedCount = selModel.getCount();
    	if(selectedCount == 0){
    		cceaf.util.ExtHelper.showInfoDialog('请选择查看的行');
    	}
    	else if(selectedCount > 1){
    		cceaf.util.ExtHelper.showInfoDialog('只能选择一行查看');
    	}
    	else{
    		//得到编辑对象的主键
    		var masterkeys = queryGrid.masterKeyIds.split(',');
    		var url = queryGrid.findByKeyAction + '?';
    		for(var i=0;i<masterkeys.length;i++){
    			var temp = selModel.getLastSelected().get(masterkeys[i]);
    			url += masterkeys[i] + '=' + temp + '&';
    		}
			//var masterkey = selModel.getLastSelected().get(queryGrid.masterKeyId);
			//var billForm = queryGrid.billForm;
    		if(queryGrid.newFormCreate == true || queryGrid.billForm == null){
	    		queryGrid.createForm();
	    	}
	    	
			queryGrid.billForm.form.reset();
			queryGrid.billForm.setReadOnly(true);
			
			var billGrid = queryGrid.billForm.down('gridpanel');
			billGrid.getStore().removeAll();
			var btnDeleteRow = billGrid.down('#btnDeleteRow');
			if (btnDeleteRow) {
				btnDeleteRow.setVisible(false);
			}
			var btnAddRow = billGrid.down('#btnAddRow');
			if (btnAddRow) {
				btnAddRow.setVisible(false);
			}
			
			var btnSave = queryGrid.billForm.down('[itemId=btnSave]');
			if (btnSave) {
				btnSave.setVisible(false);
			}
			
			if (queryGrid.cardLayout) {
				var cardPanel = queryGrid.up('panel');
				//alert(cardPanel.items.length);
				cardPanel.add(queryGrid.billForm);
				cardPanel.getLayout().setActiveItem(1);
			} else {
	    		new Ext.Window({
		        	title: title,
		        	modal: true,
		        	width: popWindowWidth,
		        	height: popWindowHeight,
		        	autoScroll:true,
		        	closeAction: 'hide',
		        	layout: 'fit',
		        	items: [queryGrid.billForm]
			    }).show();
			}
        	
        	//装载对象到表单
        	queryGrid.billForm.form.load({
		    	//url: queryGrid.findByKeyAction + '?' + queryGrid.masterKeyId + '=' + masterkey,
		    	url:  url,
		        waitMsg : '正在载入数据...',
		        success: function(form,action) { 
		        	//表单载入数据时,设置特殊值
		        	controller.loadDetailsData(action);
		        	controller.initEditPage(action);
        			
		        	//显示正确提示
		        	var jsonObject =eval("(" + action.response.responseText + ")");
		        	cceaf.util.ExtHelper.showInfoDialog(jsonObject.message);
		        },
		        failure: function(form,action) {
		        	//显示错误提示
		        	var jsonObject =eval("(" + action.response.responseText + ")");
		        	cceaf.util.ExtHelper.showErrorDialog(jsonObject.message);
		        }
		   	});
    	}
    },
    
    /**
     * 根据主键载入数据，并打开窗口
     */
    page_laod: function(title){
    	var controller = this;
    	var queryGrid = controller.getControlledView();

    	var popWindowWidth = 600;
    	var popWindowHeight = 450;
    	var popWindow = queryGrid.popWindow;
    	if(popWindow){
    		if(popWindow.width){
    			popWindowWidth = popWindow.width;
    		}
    		if(popWindow.Height){
    			popWindowHeight = popWindow.height;
    		}
    	}
    	//修正IE上的BUG
    	if (Ext.isIE) {
    		popWindowWidth += 35;
    	}
    	
    	var selModel = queryGrid.getSelectionModel();
    	
   	 	var selectedCount = selModel.getCount();
    	if(selectedCount == 0){
    		cceaf.util.ExtHelper.showInfoDialog('请选择查看的行');
    	}
    	else if(selectedCount > 1){
    		cceaf.util.ExtHelper.showInfoDialog('只能选择一行查看');
    	}
    	else{
    		//得到编辑对象的主键
    		var masterkeys = queryGrid.masterKeyIds.split(',');
    		var url = queryGrid.findByKeyAction + '?';
    		for(var i=0;i<masterkeys.length;i++){
    			var temp = selModel.getLastSelected().get(masterkeys[i]);
    			url += masterkeys[i] + '=' + temp + '&';
    		}
			//var masterkey = selModel.getLastSelected().get(queryGrid.masterKeyId);
			//var billForm = queryGrid.billForm;
    		if(queryGrid.newFormCreate == true || queryGrid.billForm == null){
	    		queryGrid.createForm();
	    	}
	    	
			queryGrid.billForm.form.reset();
			queryGrid.billForm.setReadOnly(false);
			
			var billGrid = queryGrid.billForm.down('gridpanel');
			billGrid.getStore().removeAll();
			
			if (queryGrid.cardLayout) {
				var cardPanel = queryGrid.up('panel');
				//alert(cardPanel.items.length);
				cardPanel.add(queryGrid.billForm);
				cardPanel.getLayout().setActiveItem(1);
			} else {
	    		new Ext.Window({
		        	title: title,
		        	modal: true,
		        	width: popWindowWidth,
		        	height: popWindowHeight,
		        	autoScroll:true,
		        	closeAction: 'hide',
		        	layout: 'fit',
		        	items: [queryGrid.billForm]
			    }).show();
			}
        	
        	//装载对象到表单
        	queryGrid.billForm.form.load({
		    	//url: queryGrid.findByKeyAction + '?' + queryGrid.masterKeyId + '=' + masterkey,
		    	url:  url,
		        waitMsg : '正在载入数据...',
		        success: function(form,action) { 
		        	//表单载入数据时,设置特殊值
		        	controller.loadDetailsData(action);
		        	controller.initEditPage(action);
        			
		        	//显示正确提示
		        	var jsonObject =eval("(" + action.response.responseText + ")");
		        	cceaf.util.ExtHelper.showInfoDialog(jsonObject.message);
		        },
		        failure: function(form,action) {
		        	//显示错误提示
		        	var jsonObject =eval("(" + action.response.responseText + ")");
		        	cceaf.util.ExtHelper.showErrorDialog(jsonObject.message);
		        }
		   	});
    	}
    },
    
    /**
     * 得到表格选择的行
     * 
     * @param {boolean} selectSingle 是否单行选择，默认为单行选择
     * @return {Ext.data.Model[]} 选择的行，出错返回null
     */
    getSelectedRow: function(selectSingle){
    	var controller = this;
    	var queryGrid = controller.getControlledView();
    	
    	var selection;
    	var isSelectSingle = true;
    	
    	if ( Ext.isDefined(selectSingle) ) {
    		isSelectSingle = selectSingle;
    	}
    	
    	var selModel = queryGrid.getSelectionModel();
    	var selectedCount = selModel.getCount();
    	if(selectedCount == 0){
    		cceaf.util.ExtHelper.showInfoDialog('请选择要处理的行');
    		return null;
    	}
    	else if(selectedCount > 1 && isSelectSingle){
    		cceaf.util.ExtHelper.showInfoDialog('只能选择一行处理');
    		return null;
    	}
    	else{
	    	if (isSelectSingle) {
				selection = selModel.getLastSelected();
			} else {
				selection = selModel.getSelection();
			}
		}
		
		return selection;
    },
    
    /**
     * 删除对象
     */
    page_delete: function(){
    	var controller = this;
    	var queryGrid = controller.getControlledView();
    	
    	//var queryGrid = this.findParentByType('cceafgridpanel');
    	
    	//获取要删除对象的主键集
    	var selModel = queryGrid.getSelectionModel();
    	var selectedCount = selModel.getCount();
    	if(selectedCount == 0){
    		cceaf.util.ExtHelper.showInfoDialog('请选择要删除的行');
    		return;
    	}
    	else{
    		//确定要删除对象
    		Ext.MessageBox.show({
				title:'确认提示框',
				modal: true,
				msg: '此操作不能回退，确信要删除您选中的对象吗？',
				minWidth: 200,
				buttons: Ext.MessageBox.YESNO,
				icon: Ext.MessageBox.QUESTION,
				fn:function(btn){
					if(btn == 'yes'){
						//得到删除对象的主键集
						var masterkeys = queryGrid.masterKeyIds.split(',');
						var params = {};
						for(var i=0;i<masterkeys.length;i++){
							var ids = "";
							/*
				    		selModel.getSelection().forEach(function(selection){
				    			ids = ids + selection.get(masterkeys[i]) + ",";
				    		});*/
							for(var j=0;selModel.getSelection() != null && j<selModel.getSelection().length;j++){
					    		var selection = selModel.getSelection()[j];
					    		ids = ids + selection.get(masterkeys[i]) + ",";
					    	}
							
				    		eval("params." + masterkeys[i] + "s = '" + ids + "';");
						}
						//alert(params);
						
			    		Ext.MessageBox.wait('正在处理数据...', '提示'); //进度条等待
			    		Ext.Ajax.request({
			   				url:  queryGrid.deleteAction,
			   				//params: { ids: ids },
			   				params: params,
			   				success: function(response){
			   					var isProcessSucess = cceaf.util.ExtHelper.processAjaxRequest(response,true);
			   					if(isProcessSucess){
			   						//queryGrid.getStore().load({params:{isquery:true}});
			   						var btnQuery = queryGrid.down('#btnQuery');
				    				if(btnQuery) {
				    					btnQuery.fireEvent('click');
				    				} else {
				    					queryGrid.getStore().load({params:{isquery:true}});
				    				}
				    				
			   						//如果存在树就刷新树
			   						var treePanel = Ext.getCmp("treePanel");
			   						if (treePanel) {
			   							treePanel.getStore().load();
			   						}
			   					}
			   				},
			   				failure: function(response){
			   					cceaf.util.ExtHelper.processAjaxRequest(response,false);
			   				}
						});
					}
				}
			});
    	}
    },
    
    /**
     * 注销对象
     */
    page_writeOff: function(){
    	//var queryGrid = this.findParentByType('cceafgridpanel');
    	var controller = this;
    	var queryGrid = controller.getControlledView();
    	
    	//获取要删除对象的主键集
    	var selModel = queryGrid.getSelectionModel();
    	var selectedCount = selModel.getCount();
    	var selectedCount = selModel.getCount();
    	if(selectedCount == 0){
    		cceaf.util.ExtHelper.showInfoDialog('请选择注销的行');
    	}
    	else if(selectedCount > 1){
    		cceaf.util.ExtHelper.showInfoDialog('只能选择一行注销');
    	}
    	else{
    		//确定要删除对象
    		Ext.MessageBox.show({
				title:'确认提示框',
				modal: true,
				msg: '确信要注销您选中的对象吗？',
				minWidth: 200,
				buttons: Ext.MessageBox.YESNO,
				icon: Ext.MessageBox.QUESTION,
				fn:function(btn){
					if(btn == 'yes'){
						//得到注销对象的主键集
						var masterkeys = queryGrid.masterKeyIds.split(',');
						var params = {};
						for(var i=0;i<masterkeys.length;i++){
							var ids = "";
							/*
				    		selModel.getSelection().forEach(function(selection){
				    			ids = ids + selection.get(masterkeys[i]) + ",";
				    		});*/
							for(var j=0;selModel.getSelection() != null && j<selModel.getSelection().length;j++){
					    		var selection = selModel.getSelection()[j];
					    		ids = ids + selection.get(masterkeys[i]) + ",";
					    	}
				    		eval("params." + masterkeys[i] + "s = '" + ids + "';");
						}
	
			    		Ext.MessageBox.wait('正在处理数据...', '提示'); //进度条等待
			    		Ext.Ajax.request({
			   				url:  queryGrid.writeOffAction,
			   				params: params,
			   				success: function(response){
			   					var isProcessSucess = cceaf.util.ExtHelper.processAjaxRequest(response,true);
			   					if(isProcessSucess){
			   						queryGrid.getStore().load({params:{isquery:true}});
			   					}
			   				},
			   				failure: function(response){
			   					cceaf.util.ExtHelper.processAjaxRequest(response,false);
			   				}
						});
					}
				}
			});
    	}
    },
    
    
    /**
     * ajax提交
     * ajaxParam = {
     * 		promptInfo: '删除',  //确认框提示信息
     *		selectSingle: true, //选择单行还是多行
     *		actionURL: '', //ajax的action
     *		params: {}, //提交的参数,json表达
     *      storeLoad: false, //提交后是否加载列表页面的store
     *		masterKeyIds: 'id'  //操作行的主键
     * }
     */
    page_ajax: function(ajaxParam){
    	//var queryGrid = this.findParentByType('cceafgridpanel2');
    	var controller = this;
    	var queryGrid = controller.getControlledView();
    	
    	var promptInfo;
    	var selectSingle = true;
    	var actionURL = '';
    	var params = {};
    	var storeLoad = true;
    	var masterKeyIds = queryGrid.masterKeyIds;
    	
    	if (ajaxParam) {
    		if ( Ext.isDefined( ajaxParam.promptInfo )) {
    			promptInfo = ajaxParam.promptInfo;
    		}
    		if ( Ext.isDefined( ajaxParam.selectSingle )) {
    			selectSingle = ajaxParam.selectSingle;
    		}
    		if ( Ext.isDefined( ajaxParam.actionURL )) {
    			actionURL = ajaxParam.actionURL;
    		}
    		if ( Ext.isDefined( ajaxParam.params )) {
    			params = ajaxParam.params;
    		}
    		if ( Ext.isDefined( ajaxParam.storeLoad )) {
    			storeLoad = ajaxParam.storeLoad;
    		}
    		if ( Ext.isDefined( ajaxParam.masterKeyIds )) {
    			masterKeyIds = ajaxParam.masterKeyIds;
    		}
    	}
    	if (actionURL == '') {
    		cceaf.util.ExtHelper.showInfoDialog('请设置操作的action！');
    	}
    	
    	//获取要删除对象的主键集
    	var selModel = queryGrid.getSelectionModel();
    	var selectedCount = selModel.getCount();
    	if(selectedCount == 0){
    		cceaf.util.ExtHelper.showInfoDialog('请选择要处理的行');
    	}
    	else if(selectedCount > 1 && selectSingle){
    		cceaf.util.ExtHelper.showInfoDialog('只能选择一行查看');
    	}
    	else{
    		//得到操作对象的主键集
			var masterkeys = masterKeyIds.split(',');
			for(var i=0;i<masterkeys.length;i++){
				var ids = "";
	    		
	    		if (selectSingle) {
	    			var selection = selModel.getLastSelected();
	    			ids = ids + selection.get(masterkeys[i]);
	    			eval("params." + masterkeys[i] + " = '" + ids + "';");
	    		} else {
	    			for (j=0; j<selModel.getSelection().length; j++) {
		    			var selection = selModel.getSelection()[j];
		    			ids = ids + selection.get(masterkeys[i]) + ",";
		    		}
	    			eval("params." + masterkeys[i] + "s = '" + ids + "';");
	    		}
			}
							
    		if (promptInfo) {
	    		//确定要操作的对象
	    		Ext.MessageBox.show({
					title:'确认提示框',
					modal: true,
					msg: '此操作不能回退，确信要' + promptInfo + '您选中的对象吗？',
					minWidth: 200,
					buttons: Ext.MessageBox.YESNO,
					icon: Ext.MessageBox.QUESTION,
					fn:function(btn){
						if(btn == 'yes'){
				    		Ext.MessageBox.wait('正在处理数据...', '提示'); //进度条等待
				    		Ext.Ajax.request({
				   				url:  actionURL,
				   				params: params,
				   				success: function(response){
				   					var isProcessSucess = cceaf.util.ExtHelper.processAjaxRequest(response,true);
				   					if(isProcessSucess && storeLoad){
				   						queryGrid.getStore().load({params:{isquery:true}});
				   					}
				   				},
				   				failure: function(response){
				   					cceaf.util.ExtHelper.processAjaxRequest(response,false);
				   				}
							});
						}
					}
				});
			} else {
				Ext.MessageBox.wait('正在处理数据...', '提示'); //进度条等待
	    		Ext.Ajax.request({
	   				url:  actionURL,
	   				params: params,
	   				success: function(response){
	   					var isProcessSucess = cceaf.util.ExtHelper.processAjaxRequest(response,true);
	   					if(isProcessSucess && storeLoad){
	   						queryGrid.getStore().load({params:{isquery:true}});
	   					}
	   				},
	   				failure: function(response){
	   					cceaf.util.ExtHelper.processAjaxRequest(response,false);
	   				}
				});
			}
    	}
    },
    
    
    /**
     * 统计
     */
    page_stat: function(){
    	//var queryGrid = this.findParentByType('cceafgridpanel');
    	var controller = this;
    	var queryGrid = controller.getControlledView();
    	
    	//设置页面状态为：统计页面
    	queryGrid.pageStatus = 'stat';
    	var title = '统计' + queryGrid.moduleName;
        
    	//定义统计页面
    	var statGrid = new cceaf.control.StatGridPanel({
	        columns: queryGrid.statColumns,
	        model: queryGrid.statModel,
	        statAction: queryGrid.statAction,
	        printAction: queryGrid.printStatAction,
	        
	       	//在此设置模块的自定义属性
	        moduleName: queryGrid.moduleName
    	});

    	//加载数据
    	var queryParams = {};
    	var queryAreaItems = queryGrid.query('textfield');
    	for(var i=0;queryAreaItems != null && i<queryAreaItems.length;i++){
    		var item = queryAreaItems[i];
    		itemPrefixIndex = item.getId().lastIndexOf('q_');
			if(itemPrefixIndex == 0){
				eval("queryParams." + item.getId() + " = '" + item.getValue() + "'");
			}
    	}
    	
    	statGrid.getStore().proxy.extraParams = queryParams;
    	statGrid.getStore().load({params:{isquery:true}});
    	
    	//显示窗口
    	new Ext.Window({
        	title: title,
        	modal: true,
        	width: 600,
        	height: 350,
        	autoScroll: true,
        	layout: 'fit',
        	closeAction: 'hide',
        	plain: true,
        	items: [statGrid]
    	}).show();
    	
    },
    
    /**
     * 打印列表
     */
    printList: function(){
    	//var queryGrid = this.findParentByType('cceafgridpanel');
    	var controller = this;
    	var queryGrid = controller.getControlledView();
    	
    	var url =  queryGrid.printListAction;
    	
    	cceaf.util.CceafHelper.openUrl(url);
    },
    
    /**
     * 打印表单
     */
    printBill: function(){
    	//var queryGrid = this.findParentByType('cceafgridpanel');
    	var controller = this;
    	var queryGrid = controller.getControlledView();
    	
    	var url =  queryGrid.printBillAction;
    	
    	cceaf.util.CceafHelper.openUrl(url);
    },
    
    /**
     * 导出到Excel
     */
    exportExcel: function(){
    	//var queryGrid = this.findParentByType('cceafgridpanel');
    	var controller = this;
    	var queryGrid = controller.getControlledView();
    	
    	var url =  queryGrid.exportExcelAction;
    	
    	cceaf.util.CceafHelper.openUrl(url);
    },
    
    /**
     * 新增表单，设置初始值
     */
    initAddPage: function(){
    	
    },
    
    /**
     * 表单载入数据后,设置特殊值表单值
     */
    initEditPage: function(action){
    	
    },
    
    /**
     * 加载子表数据
     */
    loadDetailsData: function(action){
    	var gridPanel = this.getControlledView();
    	var details = gridPanel.details;
    	
    	if (!details) {
    		alert('请设置grid的details（主表中子表集合字段名称）!');
    		return;
    	}
    	
    	var billGrid = gridPanel.billForm.down('gridpanel');
    	
    	Ext.MessageBox.wait('正在载入数据...', '提示'); //进度条等待
	    
    	//加载明细表数据
    	var jsonObject = Ext.JSON.decode(action.response.responseText);
    	
    	//从jsonObject中获取records
    	var jsonDetailList = eval('jsonObject.data.' + details);
    	
		if(jsonDetailList){
			billGrid.getStore().loadData(jsonDetailList,true);
		}
    	
    	Ext.MessageBox.hide();
    },
    
    /**
     * 页面载入时调用
     */
    initPage: function(){

    },
    
    /**
     * 弹出新增窗口前校验，false不弹出新增窗口
     * 
     * @return {Boolean} 校验成功返回true
     */
    checkAddPage: function(){
    	return true;
    },
    
    /**
     * 弹出修改窗口前校验，false不弹出修改窗口
     * 
     * @return {Boolean} 校验成功返回true
     */
    checkEditPage: function(){
    	return true;
    },
    
    /**
     * 弹出查看窗口前校验，false不弹出查看窗口
     * 
     * @return {Boolean} 校验成功返回true
     */
    checkViewPage: function(){
    	return true;
    }
    
});